package jdbc_firstlook;

import pojo.User;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

// 0、创建工程，导入驱动jar包
public class JDBC_ResultSet {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1、注册驱动
        //Class.forName("com.mysql.jdbc.Driver");

        //2、获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/itcase";
        String userName = "root";
        String passWord = "202111a.";
        Connection conn = DriverManager.getConnection(url,userName,passWord);

        //3、定义SQL语句
        String sql = "select * from user";

        //4、获取执行SQL对象
        Statement stmt = conn.createStatement();

        //5、执行SQL,并返回结果(影响行数)
        ResultSet resultSet = stmt.executeQuery(sql);

        List<User> list = new ArrayList<>();
        //6、处理返回结果
        while (resultSet.next()){
            User user = new User();
            String id = resultSet.getString(1);
            String name = resultSet.getString(2);
            int age = resultSet.getInt(3);

            //给对象赋值
            user.setId(id);
            user.setName(name);
            user.setAge(age);

            //把对象存储到list集合
            list.add(user);
        }

        System.out.println(list);

        //7、释放资源
        conn.close();
        stmt.close();
    }

}
